package com.bdqn.cloud.t320.controller;

import com.bdqn.cloud.t320.entity.Dto;
import com.bdqn.cloud.t320.service.UserRemoteClient;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

@RestController
public class UserController {

    //服务提供者的controller地址
    //private static final String USER_SERVICE_URL = "http://localhost:8001/list";

    private static final String USER_SERVICE_URL = "http://springcloud-service-user/list";

    @Resource
    private RestTemplate restTemplate;

    @Resource
    private UserRemoteClient userRemoteClient;

    @RequestMapping("/service/user/feign")
    public Dto getUserListFeign() {
        //调用远程的controller,restful的调用,通过feign这种声明式的远程调用
        return userRemoteClient.list();
    }

    //网络资源
    //restfull 风格的网络资源
    @RequestMapping("/service/user/list")
    public Dto getUserList() {
        ResponseEntity<Dto> entity = restTemplate.getForEntity(USER_SERVICE_URL, Dto.class);
        return entity.getBody();
    }


    /**
     * 查询所有用户的方法
     *
     * @return
     */
    @HystrixCommand(fallbackMethod = "fallback")
    @RequestMapping("/service/user/hystrix1")
    public Dto getUserListHystrix1() {
        //hystrix 默认超时时间是 1000 毫秒，如果你后端的响应超过此时间，就会触发断路器,执行fallback方法
        //1.服务超时,会降级
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //2.服务异常会降级
        //System.out.println(2/0);
        //3.远程服务userRemoteClient.list();超时、异常也会触发降级
        return userRemoteClient.list();
    }


    /**
     * 查询所有用户的方法
     *
     * @return
     */
    @HystrixCommand(fallbackMethod = "fallback")
    @RequestMapping("/service/user/hystrix2")
    public Dto getUserListHystrix2() {
        //hystrix 默认超时时间是 1000 毫秒，如果你后端的响应超过此时间，就会触发断路器,执行fallback方法
        //1.服务超时,会降级
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //2.服务异常会降级
        //System.out.println(2/0);
        //3.远程服务userRemoteClient.list();超时、异常也会触发降级
        return userRemoteClient.list();
    }


    @RequestMapping("/service/user/hystrix3")
    @HystrixCommand(fallbackMethod = "fallback",
            commandProperties={
                    @HystrixProperty(name="execution.timeout.enabled", value="true"),
                    @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds", value="5000")
            })
    public Dto getUserListHystrix3(){
        //hystrix 默认超时时间是 1000 毫秒，如果你后端的响应超过此时间，就会触发断路器,执行fallback方法
        //1.服务超时,会降级
        try {
            Thread.sleep(5500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //2.服务异常会降级
        //System.out.println(2/0);
        //3.远程服务userRemoteClient.list();超时、异常也会触发降级
        return userRemoteClient.list();
    }


    /**
     * 限流-查询所有用户的方法
     *
     * @return
     */
    @HystrixCommand(fallbackMethod = "fallback",
            threadPoolKey = "user",  //threadPoolKey 是线程池唯一标识
            //threadPoolProperties 线程池的属性
            threadPoolProperties = {
                    @HystrixProperty(name = "coreSize", value = "2"),//coreSize核心线程数大小
                    @HystrixProperty(name = "maxQueueSize", value = "2")})//maxQueueSize队列大小
    @RequestMapping("/service/user/limit")
    public Dto getUserListLimit() {
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return userRemoteClient.list();
    }



    /**
     * 接口失败执行方法
     *
     * @return
     */
    public Dto fallback() {
        Dto dto = new Dto();
        dto.setMsg("服务降级");
        return dto;
    }

}

